#ifndef __DVC_H_
#define __DVC_H_ 

#include "DVC11xx_iic.h"
#include "delay.h"

#define  CurrentSenseResistance_mR     0.5            //电流采样电阻值（单位mR)
#define  LoadDetect_Threshold 			2000
#define  uiBalanceVolDiff  				30//均衡开启压差阈值
#define  uiBalanceVol 					3000//均衡最小电芯电压
#define  GP(n)							n

void GP1_ModeConfig(uint8_t mode);
void GP2_ModeConfig(uint8_t mode);
void GP3_ModeConfig(uint8_t mode);
void GP4_ModeConfig(uint8_t mode);
void GP5_ModeConfig(uint8_t mode);
void GP6_ModeConfig(uint8_t mode);
float GPn_Analog_Input_voltage(uint8_t GP);

float DVC11XX_Calc_VCell(uint8_t cellIndex);
uint32_t DVC11XX_Calc_VBAT(void);
uint32_t DVC11XX_Calc_VLOAD(void);
uint32_t DVC11XX_Calc_VPACK(void);
uint16_t DVC11XX_Calc_V1P8(void);
void CellTestClose(uint32_t cellnumber);


float DVC11XX_Calc_CurrentWithCC1(float senseResistance_mR);
float DVC11XX_Calc_CurrentWithCC2(float senseResistance_mR);

void InterruptShield_Config(uint8_t mode);

extern void CleanError(void);
bool OCD1_Config(uint16_t TH,uint16_t DLY);
bool OCD2_Config(uint16_t TH,uint16_t DLY,bool enable);
bool OCC1_Config(uint16_t TH,uint16_t DLY);
bool OCC2_Config(uint16_t TH,uint16_t DLY,bool enable);
bool OV_Config(uint16_t TH,uint16_t DLY);
bool UV_Config(uint16_t TH,uint16_t DLY);
bool SCD_Config(uint16_t TH,uint16_t DLY,bool enable);

void Clear_OCD1(void);
void Clear_OCD2(void);
void Clear_OCC1(void);
void Clear_OCC2(void);
void Clear_OV(void);
void Clear_UV(void);
void Clear_SCD(void);

void ChargerDetect(void);

void LoadDetect(void);
void LoadPullUp(void);

void COW_enable(void);
void BrokenLineDetect(void);

void Balance_Contrl(uint32_t vlaue);


void Read_NFRT(void);
void GPM_Temperature (void);
float DVC11XX_Calc_ChipTemp(void);
float DVC11XX_Calc_BatTemp(uint8_t GP);

enum {				 //DSG Output MODE
	DSGM_CP=0,		 //电荷泵
	DSGM_FC				 //随源
};
enum {				 //CHG DSG MODE
	FET_Close=1,	 //关闭FET驱动
	FET_Close_BFCO,//关闭FET驱动，但允许在电流大于续流阈值开启FET驱动
	FET_OPEN
};		 //开启FET驱动

enum{					 //ChargePump mode
	CP_Close=0,		 //关闭电荷泵
	CP_6V,				 //电荷泵6V
	CP_7V,
	CP_8V,
	CP_9V,
	CP_10V,
	CP_11V,
	CP_12V,				 //电荷泵12V
};

typedef enum{
	CC2_1Period = 0,//1个CC2周期采1次
	CC2_2Period = 1,//1个CC2周期采1次
	CC2_4Period = 2,//1个CC2周期采1次
	CC2_8Period = 3,//1个CC2周期采1次
}DVC11xx_VadcPeriod;

//bool Get_IntFaultProt_Crc_Err_Flag();
//void Clear_IntFaultProt_Crc_Err_Flag();
bool PCHG_FETControl(bool abel);
bool PDSG_FETControl(bool abel);
bool CHG_FETControl(uint8_t mode);
bool DSG_FETControl(uint8_t mode);
bool DSGM_Control(bool mode);
bool DPC_Config(uint8_t DPC);
bool BDPT_Config(uint16_t value);
bool ChargePump_Control(uint8_t mode);
bool HSFM_Control(bool mode);
uint8_t get_dvc11xx_alert_flag();
void dvc11xx_alert_flag_clear();
void dvc_irq_init(void);
void FET_Open(void);
void GPn_ModeConfig(bool mode);
void dvc11xx_shutdown(void);
void dvc11xx_sleep(void);
void DVC11XX_init(void);
void Set_Bat_Shielding(uint8_t BatNum);
void dvc11xx_setVadcPeriod(DVC11xx_VadcPeriod period);
#endif

